<template>
  <div class="app-container" v-loading="loading">
    <div class="action-container">
      <el-button  icon="el-icon-plus" size="small" type="primary" @click="openCreatedDialog">添加文章</el-button>
     <el-button  icon="el-icon-delete" size="small" @click="deleteArticles">批量删除</el-button>
      <el-button  icon="el-icon-refresh" size="small" @click="refresh">刷新</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="categoryName"
        label="文章类别" >
      </el-table-column>
      <el-table-column
        prop="articleId"
        label="文章id" >
      </el-table-column>

      <el-table-column
        label="编辑"
        align="left" min-width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="openEditDialog(scope.row)"></el-button>
          <el-button  icon="el-icon-tickets" size="mini" type="danger" @click='jumpList(scope.row)'>文章内容</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total-elements="totalElements"
      :change-callback="getArticleList"
      ref="page"></pagination>
    <el-dialog
      title="添加文章"
      :visible.sync="createdDialog"
      :before-close="closeCreatedDialog">
      <el-form :model="form">
        <el-form-item label="类别选择" :label-width="formLabelWidth">
          <el-select v-model="form.categoryId" placeholder="请选择">
            <el-option
              v-for="item in categoryIdOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeCreatedDialog">取 消</el-button>
        <el-button type="primary" @click="addArticle" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="编辑信息"
      :visible.sync="editDialog"
      :before-close="closeEditDialog">
      <el-form :model="currentRow">
        <el-form-item label="类别选择" :label-width="formLabelWidth">
          <el-select v-model="currentRow.categoryId" placeholder="请选择">
            <el-option
              v-for="item in categoryIdOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeEditDialog">取 消</el-button>
        <el-button type="primary" @click="editFood" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {getArticleList, updateArticle, addArticle, deleteArticles} from '../api/article'
  import {selectCategory} from '../api/category'
  import pagination from '@/components/pagination/index'
  export default {
    name: 'food',
    data () {
      return {
        tableData: [], // 表格数据
        totalElements: 0, // 数据条数
        multipleSelection: [], // 选中数据-多选
        loading: true, // loading效果
        createdDialog: false, // 文章模态框
        formLabelWidth: '120px',
        // url: this.publicUrl.filesUploadUrl,
        categoryIdOptions: [],
        specListForm:{
          list:[],
        },
        form: {
          categoryId: '',
        }, // 文章数据
        imgSrc: '',
        updateSrc: '',
        specListDialog:false,//规格列表模态框
        specDialog:false,//规格添加模态框
        btnLoading: false, // 按钮loading
        editDialog: false, // 编辑模态框,
        currentRow: {
          id: '',
          articleName: '',
          categoryName: '',
          virtualSellNum: '',
          remark: '',
          logo: '',
          boMoney: '',
          categoryId: '',
          rank: '',
          foodMaterial: ''
        },
        filesData: {
          module: 'food'
        }, // 携带参数
        headers: {
          accessToken: sessionStorage.token
        } // 请求头部
      }
    },
    components: { pagination },
    created () {
      this.getArticleList()
    },
    mounted () {
      this.getArticleList()
    },
    methods: {
      handleSelectionChange (val) {
        this.multipleSelection = val
      },
      handleDelete (row) {
        this.multipleSelection.push(row)
        this.deleteArticles()
      },
      /**
       * 跳转页面
       */
      jumpList(row){
        //console.log(row);
        this.$router.push({path:'/article/views/article_content',query: {id: row.articleId}});
      },
      /*
      * 获取文章列表
      * */
      getArticleList: function (number, pageSize) {
        this.loading = true
        getArticleList(number, pageSize, this.queryList).then(response => {
          let data = response.obj
          console.log(data)
          this.totalElements = data.totalElements
          this.tableData = data.content
          setTimeout(() => {
            this.loading = false
          }, 500)
        }).catch(() => {
          this.loading = false
        })
      },
      /*
      * 刷新
      * */
      refresh () {
        this.$refs.page.refresh()
      },
      // 获取所有类别
      getcategoryIdList () {
        if (!this.categoryIdOptions.length) {
          selectCategory().then(response => {
            this.categoryIdOptions = response.obj
          }).catch(() => {})
        }
      },
      /*
      * 打开文章模态框
      * */
      openCreatedDialog () {
        this.createdDialog = true
        this.getcategoryIdList()
      },
      /*
      * 关闭文章模态框
      * */
      closeCreatedDialog () {
        this.createdDialog = false
        this.form = {
          name: '',
          amountStr: '',
          virtualSellNum: '',
          categoryId: '',
          remark: '',
          logo: '',
          rank: ''
        } // 文章数据
        this.imgSrc = ''
      },
      /*
      * 打开文章模态框
      * */
      addArticle () {
        this.loading = true
        this.btnLoading = true
        addArticle(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.closeCreatedDialog()
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },

      /*
      * 打开编辑模态框
      * */
      openEditDialog (row) {
        console.log(row)
        this.currentRow.articleId = row.articleId
        this.currentRow.categoryId = row.categoryId
        this.editDialog = true
        this.getcategoryIdList()
      },
      /*
      * 关闭编辑模态框
      * */
      closeEditDialog () {
        this.editDialog = false
      },
      /*
      * 编辑
      * */
      editFood () {
        this.loading = true
        this.btnLoading = true;
        updateArticle(this.currentRow).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.loading = false;
          this.closeEditDialog()
          this.refresh()
        }).catch(() => {
          this.loading = false
          this.btnLoading = false
        })
      },
      /*
      * 删除食品
      * */
      deleteArticles () {
        this.$confirm('是否删除这些文章？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.multipleSelection.length) {
            this.loading = true
            let ids = []
            this.multipleSelection.forEach((item) => {
              ids.push(item.articleId)
            })
            deleteArticles(ids).then(response => {
              this.$message({
                message: response.msg,
                type: 'success',
                duration: 1000
              })
              this.multipleSelection = []
              this.$refs.page.refresh()
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.$message({
              message: '请选择食品',
              type: 'warning',
              duration: 1000
            })
          }
        })
      },
    }
  }
</script>

